// pages/editAddress/editAddress.js
import {
    request
} from "../../utils/http";
let index = [18, 0, 0];

let t = 0;
let show = false;
let moveY = 200;
Page({

    /**
     * 页面的初始数据
     */
    data: {
        value: [0, 0, 0],
        provArray: [],
        cityArray: [],
        areaArray: [],
        province: "",
        city: "",
        area: "",
        provinceId: 0,
        cityId: 0,
        areaId: 0,
        receiver: "",
        mobile: "",
        addr: "",
        addrId: 0
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {
        if (options.addrId) {
            request({
                url: "/p/address/addrInfo/" + options.addrId,
            }).then(res => {
                this.setData({
                    province: res.province,
                    city: res.city,
                    area: res.area,
                    provinceId: res.provinceId,
                    cityId: res.cityId,
                    areaId: res.areaId,
                    receiver: res.receiver,
                    mobile: res.mobile,
                    addr: res.addr,
                    addrId: options.addrId
                });
                this.initCityData(res.provinceId, res.cityId, res.areaId);
            })
        } else {
            this.initCityData(this.data.provinceId, this.data.cityId, this.data.areaId);
        }
    },

    initCityData(provinceId, cityId, areaId) {
        let ths = this;
        request({
            url: "/p/area/listByPid",
            data: {
                pid: 0
            },
        }).then(res => {
            ths.setData({
                provArray: res
            });
            if (provinceId) {
                for (let index in res) {
                    if (res[index].areaId == provinceId) {
                        ths.setData({
                            value: [index, ths.data.value[1], ths.data.value[2]]
                        });
                    }
                }
            }
            ths.getCityArray(provinceId ? provinceId : res[0].areaId, cityId, areaId);
        })
    },

    //滑动事件
    bindChange(e) {
        let ths = this;
        let val = e.detail.value

        //判断滑动的是第几个column
        //若省份column做了滑动则定位到地级市和区县第一位
        if (index[0] != val[0]) {
            val[1] = 0;
            val[2] = 0;
            //更新数据
            ths.getCityArray(this.data.provArray[val[0]].areaId); //获取地级市数据
        } else { //若省份column未做滑动，地级市做了滑动则定位区县第一位
            if (index[1] != val[1]) {
                val[2] = 0;
                //更新数据
                ths.getAreaArray(this.data.cityArray[val[1]].areaId); //获取区县数据
            } else {

            }
        }
        index = val;
        this.setData({
            value: [val[0], val[1], val[2]],
        })
        ths.setData({
            province: ths.data.provArray[ths.data.value[0]].areaName,
            city: ths.data.cityArray[ths.data.value[1]].areaName,
            area: ths.data.areaArray[ths.data.value[2]].areaName,
            provinceId: ths.data.provArray[ths.data.value[0]].areaId,
            cityId: ths.data.cityArray[ths.data.value[1]].areaId,
            areaId: ths.data.areaArray[ths.data.value[2]].areaId
        })
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {
        this.animation = wx.createAnimation({
            transformOrigin: "50% 50%",
            duration: 0,
            timingFunction: "ease",
            delay: 0
        })
        this.animation.translateY(200 + 'vh').step();
        this.setData({
            animation: this.animation.export(),
            show: show
        })
    },

    //移动按钮点击事件
    translate(e) {
        if (t == 0) {
            moveY = 0;
            show = false;
            t = 1;
        } else {
            moveY = 200;
            show = true;
            t = 0;
        }
        this.setData({
            show: true
        });
        // this.animation.translate(arr[0], arr[1]).step();
        this.animationEvents(this, moveY, show);

    },
    //隐藏弹窗浮层
    hiddenFloatView(e) {
        //console.log(e);
        moveY = 200;
        show = true;
        t = 0;
        this.animationEvents(this, moveY, show);

    },

    //动画事件
    animationEvents(that, moveY, show) {
        //console.log("moveY:" + moveY + "\nshow:" + show);
        that.animation = wx.createAnimation({
            transformOrigin: "50% 50%",
            duration: 400,
            timingFunction: "ease",
            delay: 0
        })
        that.animation.translateY(moveY + 'vh').step()

        that.setData({
            animation: that.animation.export()
        })

    },

    /**
     * 根据省份ID获取 城市数据
     */
    getCityArray(provinceId, cityId, areaId) {
        let ths = this;
        request({
            url: "/p/area/listByPid",
            data: {
                pid: provinceId
            },
        }).then(res => {
            ths.setData({
                cityArray: res
            });
            if (cityId) {
                for (let index in res) {
                    if (res[index].areaId == cityId) {
                        ths.setData({
                            value: [ths.data.value[0], index, ths.data.value[2]]
                        });
                    }
                }
            }
            ths.getAreaArray(cityId ? cityId : res[0].areaId, areaId);
        })
    },

    /**
     * 根据城市ID获取 区数据
     */
    getAreaArray(cityId, areaId) {
        let ths = this;
        request({
            url: "/p/area/listByPid",
            data: {
                pid: cityId
            },
        }).then(res => {
            ths.setData({
                areaArray: res
            });
            if (areaId) {

                for (let _index in res) {
                    if (res[_index].areaId == areaId) {
                        ths.setData({
                            value: [ths.data.value[0], ths.data.value[1], _index]
                        });
                    }
                }

                index = ths.data.value;

                ths.setData({
                    province: ths.data.province,
                    city: ths.data.city,
                    area: ths.data.area,
                    provinceId: ths.data.provinceId,
                    cityId: ths.data.cityId,
                    areaId: ths.data.areaId
                })

            } else {
                ths.setData({
                    province: ths.data.provArray[ths.data.value[0]].areaName,
                    city: ths.data.cityArray[ths.data.value[1]].areaName,
                    area: ths.data.areaArray[ths.data.value[2]].areaName,
                    provinceId: ths.data.provArray[ths.data.value[0]].areaId,
                    cityId: ths.data.cityArray[ths.data.value[1]].areaId,
                    areaId: ths.data.areaArray[ths.data.value[2]].areaId
                })
            }
        })
    },

    bindRegionChange(e) {
        //console.log('picker发送选择改变，携带值为', e.detail.value)
        this.setData({
            region: e.detail.value
        })
    },


    /**
     * 保存地址
     */
    onSaveAddr() {
        let ths = this;
        let receiver = ths.data.receiver;
        let mobile = ths.data.mobile;
        let addr = ths.data.addr;

        if (!receiver) {
            wx.showToast({
                title: '请输入收货人姓名',
                icon: "none"
            })
            return;
        }
        if (!mobile) {
            wx.showToast({
                title: '请输入手机号码',
                icon: "none"
            })
            return;
        }
        let regexp = /^[1]([3-9])[0-9]{9}$/;
        if (!regexp.test(mobile)) {
            wx.showToast({
                title: '请输入正确的手机号码',
                icon: "none"
            })
            return;
        }
        if (!addr) {
            wx.showToast({
                title: '请输入详细地址',
                icon: "none"
            })
            return;
        }
        let url = "/p/address/addAddr";
        let method = "POST";
        if (ths.data.addrId != 0) {
            url = "/p/address/updateAddr";
            method = "PUT";
        }
        //添加或修改地址
        request({
            url: url,
            method: method,
            data: {
                receiver: ths.data.receiver,
                mobile: ths.data.mobile,
                addr: ths.data.addr,
                province: ths.data.province,
                provinceId: ths.data.provinceId,
                city: ths.data.city,
                cityId: ths.data.cityId,
                areaId: ths.data.areaId,
                area: ths.data.area,
                userType: 0,
                addrId: ths.data.addrId
            },
        }).then(res => {
            wx.navigateBack({
                delta: 1
            })
        })
    },

    onReceiverInput(e) {
        this.setData({
            receiver: e.detail.value
        });
    },

    onMobileInput(e) {
        this.setData({
            mobile: e.detail.value
        });
    },

    onAddrInput(e) {
        this.setData({
            addr: e.detail.value
        });
    },

    //删除配送地址
    onDeleteAddr(e) {
        let ths = this;
        wx.showModal({
            title: '',
            content: '确定要删除此收货地址吗？',
            confirmColor: "#eb2444",
            success(res) {
                if (res.confirm) {
                    let addrId = ths.data.addrId;
                    request({
                        url: "/p/address/deleteAddr/" + addrId,
                        method: "DELETE",
                    }).then(res => {
                        wx.navigateBack({
                            delta: 1
                        })
                    })
                } else if (res.cancel) {
                    console.log('用户点击取消')
                }
            }
        })

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage() {

    }
})